package third.lib.wx.login.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestClientException;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.wx.common.utils.WXUtils;
import third.lib.wx.login.common.config.WxLoginConfig;
import third.lib.wx.login.common.model.WxAccessToken;
import third.lib.wx.login.common.model.WxUserInfo;

public class WxLogin {
    private final Logger logger = LoggerFactory.getLogger(WxLogin.class);

    protected WxLoginConfig config;

    @Autowired
    ThirdLogger thirdLogger;

    protected WxLoginConfig getWxLoginConfig(){
        return config;
    }

    public WxAccessToken getAccessToken(String code) throws ThirdException {
        thirdLogger.info("微信登陆配置: " + JSON.toJSONString(getWxLoginConfig()));
        if (getWxLoginConfig().getAppId() != null && !getWxLoginConfig().getAppId().isEmpty()) {
            if (getWxLoginConfig().getSecret() != null && !getWxLoginConfig().getSecret().isEmpty()) {
                String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", this.getWxLoginConfig().getAppId(), this.getWxLoginConfig().getSecret(), code);
                String json = null;

                try {
                    json = (String)WXUtils.getRestTemplate().getForObject(url, String.class, new Object[0]);
                } catch (RestClientException var6) {
                    var6.printStackTrace();
                    throw new ThirdException(ThirdStatus.NET);
                }

                JSONObject obj = JSON.parseObject(json);
                if (obj.get("errcode") != null) {
                    this.logger.error(json);
                    throw new ThirdException(ThirdStatus.WX);
                } else {
                    WxAccessToken accessToken = new WxAccessToken();
                    accessToken.setAccessToken(obj.get("access_token").toString());
                    accessToken.setExpiresIn(Integer.parseInt(obj.get("expires_in").toString()));
                    accessToken.setRefreshToken(obj.get("refresh_token").toString());
                    accessToken.setOpenId(obj.get("openid").toString());
                    return accessToken;
                }
            } else {
                throw new ThirdException(ThirdStatus.WX);
            }
        } else {
            throw new ThirdException(ThirdStatus.WX);
        }
    }

    public WxUserInfo getWXUserInfo(String token, String openId) throws ThirdException {
        String url = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", token, openId);
        String json = null;

        try {
            json = (String)WXUtils.getRestTemplate().getForObject(url, String.class, new Object[0]);
        } catch (RestClientException var7) {
            var7.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        }

        WxUserInfo userInfo = null;
        JSONObject obj = JSON.parseObject(json);
        if (obj.get("errcode") != null) {
            this.logger.error(json);
            throw new ThirdException(ThirdStatus.WX);
        } else {
            userInfo = new WxUserInfo();
            userInfo.setOpenId(obj.get("openid").toString());
            userInfo.setNickName(obj.get("nickname").toString());
            userInfo.setSex(Integer.parseInt(obj.get("sex").toString()));
            userInfo.setProvince(obj.get("province").toString());
            userInfo.setCity(obj.get("city").toString());
            userInfo.setCountry(obj.get("country").toString());
            userInfo.setHeadImgUrl(obj.get("headimgurl").toString());
            userInfo.setUnionId(obj.get("unionid").toString());
            return userInfo;
        }
    }
}